package com.kaikeba.t31.ticket.controller;

import com.github.pagehelper.PageInfo;
import com.kaikeba.t31.admin.po.AdminUser;
import com.kaikeba.t31.core.controller.BaseController;
import com.kaikeba.t31.ticket.client.UserClient;
import com.kaikeba.t31.ticket.po.Passenger;
import com.kaikeba.t31.ticket.service.IPassengerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/passenger")
public class PassengerController extends BaseController<IPassengerService, Passenger> {
    @Autowired
    private UserClient userClient;

    @Override
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/list-page")
    public PageInfo<Passenger> listPage(Passenger entity,
                                        @RequestParam(name = "page", defaultValue = "1", required = false) int page,
                                        @RequestParam(name = "rows", defaultValue = "10", required = false) int rows){
        // 根据用户名得到user，然后通过user_id查询乘客
        AdminUser user = userClient.getByUserName(SecurityContextHolder.getContext().getAuthentication().getName());
        entity.setUserId((user.getId()));

        PageInfo<Passenger> result = service.listPage(entity,page,rows);
        return result;
    }

    @Override
    public void beforeSave(Passenger entity) throws Exception {
        AdminUser user = userClient.getByUserName((SecurityContextHolder.getContext().getAuthentication().getName()));
        entity.setUserId(user.getId());
    }
}
